<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;

$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return=array();
if($_RQ['op']=='main'){
	$return['status']=10;
	$this->session('ismyitem',intval($_RQ['ismyitem']));
	exi($return);
}
/*******op*******/
if($_RQ['op']=='list'){
	//查询进入该页面的客户所有订单
	$params=array('uniacid'=>$_SESSION['uniacid']);
	$ismyitem=$this->session('ismyitem');
	if($ismyitem){
		$pids = pdo_getall('jy_wuliu_item',array('mid'=>$_SESSION['uid']),array('id'),'id');
		if($pids){
			$pids=array_keys($pids);
			$params['pid']=$pids;
		}
	}else{
		$params['mid']=$_SESSION['uid'];
	}
	if(isset($_RQ['status']) && intval($_RQ['status'])<10){
		$params['status']=intval($_RQ['status']);
		$return['status']=$_RQ['status'];
	}elseif($_RQ['status']=='10'){
		$return['status']=10;
	}else{
		$return['status']=1;
		$params['status']=1;
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$list=pdo_getslice('jy_wuliu_feelog',$params,array($pindex,$psize),$total,$fields,'','createtime desc');
	if($list){
			foreach($list as &$item){
				$user=pdo_get('core_members',array('id'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']));
				$info=pdo_get('jy_wuliu_item',array('id'=>$item['pid'],'uniacid'=>$_SESSION['uniacid']));
				$item['user']=$user;
				$info['title']=explode('&#8644',$info['title']);
				$item['info']=$info;
				$item['orderid']=$item['id'];
				$item['typestatus']=$item['status']==0?'待支付':'已完成';
				$item['typetitle']=$this->item_types[$info['type']];
				$item['createtime']=date('Y-m-d', $item['createtime']);
			}
	}
	$return['list']=$list;
	exi($return);
}
if($_RQ['op']=='jielilist'){
	//查询进入该页面的客户所有订单
	$params=array('uniacid'=>$_SESSION['uniacid']);
	$ismyitem=$this->session('ismyitem');
	if($ismyitem){
		$pids = pdo_getall('jy_wuliu_item',array('mid'=>$_SESSION['uid']),array('id'),'id');
		if($pids){
			$pids=array_keys($pids);
			$params['pid']=$pids;
		}
		
	}else{
		$params['mid']=$_SESSION['uid'];
	}
	if(isset($_RQ['status']) && intval($_RQ['status'])<10){
		$params['status']=intval($_RQ['status']);
		$return['status']=$_RQ['status'];
	}else{
		$return['status']=10;
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$list=pdo_getslice('jy_wuliu_item_jieli',$params,array($pindex,$psize),$total,$fields,'','createtime desc');
	if($list){
			foreach($list as &$item){
				$user=pdo_get('core_members',array('id'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']));
				$info=pdo_get('jy_wuliu_item',array('id'=>$item['pid'],'uniacid'=>$_SESSION['uniacid']));
				$item['user']=$user;
				$info['title']=explode('&#8644',$info['title']);
				$item['info']=$info;
				$order=pdo_get('jy_wuliu_feelog',array('id'=>$item['orderid'],'uniacid'=>$_SESSION['uniacid']));
				$item['fee']=$order['fee'];
				$item['typestatus']=$order['status']==0?'待支付':'已完成';
				$item['typetitle']=$this->item_types[$info['type']];
				$item['createtime']=date('Y-m-d', $item['createtime']);
			}
	}
	$return['list']=$list;
	exi($return);
}
//订单详情
if($_RQ['op']=='detail'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择','error');
	}
	$params=array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']);
	if($_RQ['isfee']){
		$order=pdo_get('jy_wuliu_feelog',$params);
		$order['orderid']=$_RQ['id'];
	}else{
		$order=pdo_get('jy_wuliu_item_jieli',$params);
	}
	//用户信息
	$member=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['mid']));
	//项目信息
	$item=pdo_get('jy_wuliu_item',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['pid']));	
	//自定义字段数据
	$order['groupsendsettitle']=$item['groupsendset']==1?'快递发货':$item['groupsendset']==0?'无需物流':'自提点提货';
	$formdata=pdo_get('jy_wuliu_item_formdata',array('mid'=>$order['mid'],'pid'=>$order['pid'],'uniacid'=>$_SESSION['uniacid']));
	$order['data']=json_decode($formdata['data'],true);
	$order['diydata']=json_decode($formdata['diydata'],true);
	if($item['status']==0){
		$item['typestatus']='待支付';
	}
	if($item['status']==1){
		$item['typestatus']='已支付';
	}
	if($item['status']==2){
		$item['typestatus']='已发货';
	}
	if($order['status']>=3){
		$order['typestatus']=$order['status']==3?'已取消':'已完成';
	}
	if($order['status']>=2){
				$order['express']=pdo_get('jy_wuliu_order_express',array('order_id'=>$order['id'],'uniacid'=>$_SESSION['uniacid']));
				$order['express']['express_name']=$return['express'][$order['express']['express_id']-1]['name'];
	}
	$order['typetitle']=$this->item_types[$order['type']];
	$order['createtime']=date('Y-m-d H:i:s', $order['createtime']);
	$order['sendtime']=date('Y-m-d H:i:s', $order['sendtime']);
	$order['paytime']=date('Y-m-d H:i:s', $order['paytime']);
	$return['order']=$order;
	$return['member']=$member;
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='receive'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择订单','error');
	}
	if($_RQ['isfee']){
		pdo_update('jy_wuliu_feelog',array('status'=>'2'),array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
		pdo_update('jy_wuliu_item_jieli',array('status'=>'2'),array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$_RQ['id']));
	}else{
		pdo_update('jy_wuliu_item_jieli',array('status'=>'2'),array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	}
	exi('确认凭证成功');
}
if($_RQ['op']=='del'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择订单','error');
	}
	pdo_delete('jy_wuliu_feelog',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	exi('删除凭证成功');
}
if($_RQ['op']=='pay'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择订单','error');
	}
	$order=pdo_get('jy_wuliu_feelog',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	$title=pdo_getcolumn('jy_wuliu_item',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['pid']),'title');
	$typetitle=$this->item_types[$order['type']];
	$params['fee']=$order['fee'];
	$params['title']=$typetitle.'拼车订单';
	$params['id']=$_RQ['id'];
	$params['plugin']='jy_wuliu';
	$params['back_url']='jy_wuliu/feelog/payresult';
	$payid=cfc('pay')->create_pay_order($params);
	exi($payid);
}
if($_RQ['op']=='payresult'){
	$payresult=$_RQ['pay_result'];
	if(!$payresult){
		exi('支付失败，未收到支付结果！','error','list');
	}
	if($payresult['status']!=1){
		exi('支付失败！','error','list');
	}
	$order=pdo_get('jy_wuliu_feelog',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	if(!$order){
		exi('支付失败！指定订单不存在！','error');
	}
	if($order['fee']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！','error');
	}else{
			pdo_update('jy_wuliu_feelog',array('paytype'=>$payresult['pay_type'],'ispay'=>1,'paytime'=>time(),'status'=>1),array('id'=>$payresult['id']));
			//发信息
			//$this->notice('order_pay',$_SESSION['uid'],$store_id,$payresult['id']);
			//打印
			//$this->printmsg($text,$order['id'],1);
			//添加接力数据：
			$item=pdo_get('jy_wuliu_item',array('id'=>$order['pid'],'uniacid'=>$_SESSION['uniacid']));
			$insert=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$_SESSION['uid'],
				'pid'=>$item['id'],
				'snsid'=>$item['snsid'],
				'type'=>$item['type'],
				'status'=>1,
				'orderid'=>$payresult['id'],
				'createtime'=>TIMESTAMP
			);
			pdo_insert('jy_wuliu_item_jieli',$insert);
			exi('拼车成功!','success','jy_wuliu/feelog/');
	}
}